var id = ""         #此MOD创意工坊ID，可选，暂不使用
var name = "g_ZHPLine" #此文件名称
var test = false    #是否开启测试，推荐格式，非必要
var data = []       #想要支持硬盘保存的数据，也可以改成字典或其他类型
var base = null     #基础工具变量名
var Utils = null
var Shop = null
var charas = []#选池生物
var items = []#选池装备
var path

func _ready():
	pass

func _init():
	call_deferred("Init")

func Init():
	if globalData.infoDs.has("g_ZHPBase"):
		base = globalData.infoDs["g_ZHPBase"]
	if base != null:
		path = chaData.infoDs["cex___zahuopu-Hide"].dir
		Utils = globalData.infoDs["g_ZHPUtils"]
		Shop = globalData.infoDs["g_ZHPShop"]
		base.setting[name] = data
		base.connect("onStartGame",self,"StartGame")
		base.connect("onNewGame",self,"NewGame")
		base.connect("backTitle",self,"backTitle")

signal resetData

func backTitle():
	emit_signal("resetData")

func StartGame():
	resetData()
	if base.setting.get("zhp") == true:
		sys.main.connect("onBattleReady",self,"BattleReady")
		if base.setting.get("CheckMode") == true:
			sys.main.connect("onBattleEnd",self,"BattleEnd")
	if base.setting.get("zhiyuan") == true:
		sys.main.connect("onCharaDel",self,"CharaDel")
	if base.setting.get("fc") == true:
		battleUI()

func NewGame():
	kaiju()

func kaiju():
	yield(sys.get_tree().create_timer(0.1), "timeout")
	if base.setting.get("zhp") == true and base.setting.get("CheckMode") == false:
		sys.main.player.addCha(sys.main.newChara("cex___zahuopu-Hide"))
	if base.setting.get("zhp") == true and base.setting.get("CheckMode") == true:
		setGroceryStore()
	if base.setting.get("badao") == true:
		for i in sys.main.btGrid.get_children():
			sys.main.btGrid.remove_child(i)
		for j in sys.main.itemGrid.get_children():
			sys.main.itemGrid.remove_child(j)
		sys.main.player.addCha(sys.main.newChara("cex___E-Hide"))

func battleUI():
	var uiBt = Button.new()
	uiBt.text = tr("SETTEXT_6_1")
	uiBt.margin_left = 1230
	uiBt.margin_top = 600
	sys.main.get_node("ui").add_child(uiBt)
	uiBt.connect("pressed", self, "cellcha")

func resetData():
	readHp()
	addlv4()
	torfx = false
	torfy = false

var torfx = false
var torfy = false

func BattleReady():
	if Shop.exist == false:return
	if base.setting.get("CheckMode") == true:return
	for i in sys.main.btGrid.get_children():
		var chas:Chara = i.get_child(0)
		if chas.id == "cex___zahuopu-Hide":
			torfx = true
			break
	for k in Utils.getAllCells(1):
		var cha = sys.main.matCha(k)
		if cha != null and cha.team != 2 and cha.id == "cex___zahuopu-Hide":
			torfy = true
			break
	if torfx == false and torfy == false:
		Shop.exist = false

func BattleEnd():
	setGroceryStore()

func CharaDel(cha):
	var chas = cha.getAllChas(2)
	if cha.team == 2:return
	if chas.size() == 1 and cha.isDeath == true:
		if sys.main.player.maxHp > sMaxHp:
			readHp()
		if sys.main.player.maxHp > lMaxHp:
			sys.main.player.maxHp -= lMaxHp
			sys.main.player.hp *= sys.main.player.maxHp/rMaxHp
			rMaxHp = sys.main.player.maxHp
			setchara("cex___Reaper-Hide",1)

var cellitem = ["i_Hide-ZHP-UnbreakableStrength","i_Hide-ZHP-UnbreakableHeart","i_Hide-ZHP-Kingsoul"]

func cellcha():
	if not sys.main.isAiStart:
		sys.newBaseMsg(tr("SETTEXT_14"),tr("SETTEXT_15"))
		return
	setchara(sys.rndListItem(Utils.lv4),1)

func setchara(id,team):
	var cha
	for cell in Utils.getAllCells(team):
		if sys.main.matCha(cell) == null:
			cha = sys.main.newChara(id,team)
			sys.main.map.add_child(cha)
			sys.main.setMatCha(cell, cha)
			cha.isSumm = true
			cha.isDrag = true
			cha.addItem(sys.newItem("i_Hide-ZHP-Kingsoul"))
			break
	return cha

var sMaxHp#玩家初始maxHp
var lMaxHp#5%maxHp
var rMaxHp#当前maxHp
func readHp():
	sMaxHp = sys.main.player.maxHp
	lMaxHp = sMaxHp*0.05
	rMaxHp = sMaxHp

func addlv4():
	Utils.lv4.clear()
	for i in chaData.rndPool.items:
		match i[0].lv:
			4:
				Utils.lv4.append(i[0].id)

func setGroceryStore():
	var cha = sys.main.newChara("cex___zahuopu-Hide",1)
	sys.main.map.add_child(cha)
	sys.main.setMatCha(Vector2(7,3), cha)
	cha.isSumm = true
	cha.isDrag = false
	# var sGS = TextureButton.new()
	# var jpg = Image.new()
	# jpg.load(path+"/cex___zahuopu-Hide/cha.png")
	# var djpg = ImageTexture.new()
	# djpg.create_from_image(jpg)
	# djpg.flags = 4
	# sGS.set_normal_texture(djpg)
	# sys.main.get_node("ui").add_child(sGS)
	# sGS.set_size(Vector2(100,100))
	# sGS.set_position(Vector2(800,500))
	# sGS.set_visible(true)
	# sGS.connect("pressed", self, "shop")

func shop():
	sys.newBaseMsg("","制作中")